#!/usr/bin/env python
# -*- coding: utf-8 -*-

import datetime

class Converter(object):
    """common converter"""    

    @staticmethod
    def numberToDateString(num):
        '''
        Convert js date number to date string format 'yyyy-MM-dd'

        Examples: numberToDateString(1395014400000)
        >>>2014-03-17
        '''
        if num and num > 0:
            return datetime.date.fromtimestamp(num/1000).strftime("%Y-%m-%d")
        else:
            return None

    @staticmethod
    def boolStrToBit(str):
        '''
        Convert bool string to bit 
        '''
        if str == "true" or str == True:
            return 1
        else:
            return 0    

    @staticmethod
    def dicToTupleList(items, propertiesArray):
        return [Converter.dicToTupleWithOrder(item, propertiesArray) for item in items]

    @staticmethod
    def dicToTupleWithOrder(item, propertiesArray):
        values = [item[propertyName] for propertyName in propertiesArray]
        return tuple(values)

    @staticmethod
    def xstr(s):
        '''
        Return empty string for None, other would call str.
        '''
        if s is None:
            return ''
        return str(s)

    @staticmethod
    def partition_list(list, chunkSize):
        '''
        partition a list to equal sized chunks
        '''
        for i in range(0, len(list), chunkSize):
            yield list[i:i+chunkSize]
